package com.cardbaobao.cardbabyclient.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.ui.activity.CreditDetailActivity;
import com.cardbaobao.cardbabyclient.ui.activity.LoansDetailActivity;
import com.cardbaobao.cardbabyclient.ui.activity.web.EventWebViewActivity;
import com.cardbaobao.cardbabyclient.base.Constant;
import com.cardbaobao.cardbabyclient.model.IndexLayout;
import com.cardbaobao.cardbabyclient.utils.ImageLoaderUtil;
import com.cardbaobao.cardbabyclient.utils.URLUtil;

import java.util.List;

/**
 * Description:
 * Created by FHW
 * on 2017/6/13
 */

public class CardGiftRcAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int TYPE_BIG = 0xff01;//长图
    public static final int TYPE_DEFAULT = 0xff02;//正常
    private List<IndexLayout.HomeTabBean> mDate = null;
    //    private List<String> msmallData = null;
    private Context mContext;

    //    public CardGiftRcAdapter(Context context, List data, List smallData) {
//        this.mContext = context;
//        this.mDate = data;
//        this.msmallData = smallData;
//
//    }
    public CardGiftRcAdapter(Context context, List data) {
        this.mContext = context;
        this.mDate = data;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_BIG:
                return new HolderViewBig(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_card_gift_big_rv, parent, false));
            case TYPE_DEFAULT:
                return new HolderViewSmall(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_card_gift_rv, parent, false));
            default:
                return null;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HolderViewBig) {
            bindTypeViewBig((HolderViewBig) holder, position);
        } else if (holder instanceof HolderViewSmall) {
            bindTypeViewSmall((HolderViewSmall) holder, position);
        }
    }

    private void bindTypeViewSmall(HolderViewSmall holder, final int position) {
        ImageLoaderUtil.displayImage(URLUtil.CBB_IMG_URL + mDate.get(position).getPicture_path(), holder.iv_pic, 16);
        holder.tv_title.setText(mDate.get(position).getTopical());
        holder.tv_marketing_one.setText(mDate.get(position).getMarketing_one());
        holder.ll_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setGoTOView(position);
            }
        });
    }

    private void bindTypeViewBig(HolderViewBig holder, final int position) {
        ImageLoaderUtil.displayImage(URLUtil.CBB_IMG_URL + mDate.get(position).getPicture_path(), holder.user_avatar);
        holder.tv_title.setText(mDate.get(position).getTopical());
        holder.tv_marketing_one.setText(mDate.get(position).getMarketing_one());
        holder.tv_marketing_two.setText(mDate.get(position).getMarketing_two());
        holder.rl_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setGoTOView(position);
            }
        });
    }

    private void setGoTOView(int position) {
        Intent mIntent = new Intent();
        switch (mDate.get(position).getPage_type()) {
            case 0://原生页面
                switch (mDate.get(position).getDetail_code()){
                    case Constant.ZCC0101://信用卡
                        mIntent.setClass(mContext, CreditDetailActivity.class);
                        mIntent.putExtra(CreditDetailActivity.CARD_ID_S,
                                mDate.get(position).getDetail_id());
                        mIntent.putExtra(CreditDetailActivity.SERIES_CARD_ID_S,
                                mDate.get(position).getDetail_id());
                        mIntent.putExtra(CreditDetailActivity.CARD_NAME_S,
                                mDate.get(position).getTopical());
                        mIntent.putExtra(CreditDetailActivity.FROM_PAGE_S,
                                "首页");
                        mContext.startActivity(mIntent);
                        break;
                    case Constant.ZCC0102://贷款
                        mIntent.setClass(mContext, LoansDetailActivity.class);
                        mIntent.putExtra(LoansDetailActivity.LOANS_ID_S,
                                mDate.get(position).getDetail_id());
                        mIntent.putExtra(LoansDetailActivity.LOANS_NAME_S,
                                mDate.get(position).getTopical());
                        mIntent.putExtra(LoansDetailActivity.FROM_PAGE_S,"首页");
                        mContext.startActivity(mIntent);
                        break;
                }
                break;
            case 1://H5页面上传
            case 2://
                mIntent.setClass(mContext, EventWebViewActivity.class);
                mIntent.putExtra("url",mDate.get(position).getUrl());
                mIntent.putExtra(EventWebViewActivity.WEB_TITLE_S, mDate.get(position).getTopical());
                mIntent.putExtra(EventWebViewActivity.WEB_TYPE_I, 8);
                mContext.startActivity(mIntent);
                break;
        }
    }

    @Override
    public int getItemViewType(int position) {
        return ((position + 1) % 5) == 0 ? TYPE_BIG : TYPE_DEFAULT;
    }

    @Override
    public int getItemCount() {
        return mDate == null ? 0 : mDate.size();
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);

        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = ((GridLayoutManager) manager);
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    int type = getItemViewType(position);
                    switch (type) {
                        case TYPE_BIG:
                            return gridManager.getSpanCount();
                        case TYPE_DEFAULT:
                            return 3;
                        default:
                            return 3;
                    }
                }
            });
        }
    }

    public class HolderViewBig extends RecyclerView.ViewHolder {
        RelativeLayout rl_content;
        TextView tv_title;
        TextView tv_marketing_one;
        TextView tv_marketing_two;
        ImageView user_avatar;

        public HolderViewBig(View itemView) {
            super(itemView);
            rl_content = (RelativeLayout) itemView.findViewById(R.id.rl_content);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            tv_marketing_one = (TextView) itemView.findViewById(R.id.tv_marketing_one);
            tv_marketing_two = (TextView) itemView.findViewById(R.id.tv_marketing_two);
            user_avatar = (ImageView) itemView.findViewById(R.id.user_avatar);
        }
    }

    public class HolderViewSmall extends RecyclerView.ViewHolder {
        TextView tv_title;
        TextView tv_marketing_one;
        ImageView iv_pic;
        LinearLayout ll_content;

        public HolderViewSmall(View itemView) {
            super(itemView);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            tv_marketing_one = (TextView) itemView.findViewById(R.id.tv_marketing_one);
            iv_pic = (ImageView) itemView.findViewById(R.id.iv_pic);
            ll_content = (LinearLayout) itemView.findViewById(R.id.ll_content);
        }
    }

}
